package top.csjss.hospital.controller;

import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.csjss.hospital.entity.SysRole;
import top.csjss.hospital.enums.HospitalExceptionEnums;
import top.csjss.hospital.service.SysRoleService;
import top.csjss.hospital.vo.LayUIDTreeVO;
import top.csjss.hospital.vo.R;
import top.csjss.hospital.vo.SysRoleVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ztf
 * @Date 20:05 2021/10/18
 **/
@RestController
public class RoleController {

    private Logger log= LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("/role/loadAllRole")
    public R loadAllRole(String rolename, String roledesc, Integer available, @RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer limit){
        //1.调用service查询角色信息
        PageInfo<SysRole> pageInfo=sysRoleService.loadAllRole(rolename,roledesc,available,page,limit);
        //封装数据
        long count = pageInfo.getTotal();
        List<SysRoleVO> data = new ArrayList<>();
        for (SysRole sysRole : pageInfo.getList()) {
            SysRoleVO vo = new SysRoleVO();
            BeanUtils.copyProperties(sysRole, vo);
            data.add(vo);
        }

        //返回
        return R.ok(count,data);
    }

    /**
     * 角色分配菜单获取
     * @param roleid
     * @return
     */
    @PostMapping("/role/initRoleMenuTreeJson")
    public R initRoleMenuTreeJSON(Integer roleid){
        //校验参数
        if (roleid==null){
            log.info("【分配角色菜单】请求参数异常");
            R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR);
        }
        //调用service查询全部菜单信息
        List<LayUIDTreeVO> data=sysRoleService.initRoleMenuTreeJSON(roleid);
        //返回
        return R.ok(data);
    }

    /**
     * 保存角色分配
     * @param roleid
     * @param ids
     * @return
     */
    @PostMapping("/role/saveRoleMenu")
    public R saveRoleMenu(Integer roleid,Integer[] ids){
        if (roleid==null){
            log.info("【保存角色分配】请求参数异常");
            R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR);
        }
        //调用service
        sysRoleService.saveRoleMenu(roleid,ids);
        //响应
        return R.ok("分配角色菜单成功");
    }

    /**
     * 删除角色信息(逻辑删除)
     * @param roleid
     * @return
     */
    @PostMapping("/role/deleteRole")
    public R deleteRole(Integer roleid){
        if (roleid==null){
            log.info("【角色删除】请求参数异常");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR);
        }
        //调用service
        sysRoleService.deleteRole(roleid);
        //响应
        return R.ok("角色删除成功");
    }

    /**
     * 添加角色
     * @param rolename
     * @param roledesc
     * @param available
     * @return
     */
    @PostMapping("/role/insertRole")
    public R insertRole(String rolename,String roledesc,Integer available){
//        校验参数
        if (rolename==null||available==null){
            log.info("【角色添加】请求参数异常");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR);
        }
        //调用service
        sysRoleService.insertRole(rolename,roledesc,available);

        //响应
        return R.ok("角色添加成功");
    }
    @PostMapping("/role/updateRole")
    public R updateRole(Integer roleid,String rolename,String roledesc,Integer available){
        //校验参数
        if (rolename==null||available==null||roleid==null){
            log.info("【角色修改】请求参数异常");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR);
        }
        //调用service
        sysRoleService.updateRole(roleid,rolename,roledesc,available);

        return R.ok("角色修改成功");
    }

}
